package com.jerei.plugin.dao.impl;

import com.jerei.plugin.dao.IWcmCommonRoleMenuDetailDao;
import com.jerei.plugin.entity.WcmCommonRoleMenuDetail;
import com.jerei.plugin.utils.JrJdbcTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * Created by sunfeihu on 2016/12/7.
 */
@Repository
public class WcmCommonRoleMenuDetailDaoImpl implements IWcmCommonRoleMenuDetailDao {
    @Autowired
    private JrJdbcTemplate jrJdbcTemplate;

    @Override
    public WcmCommonRoleMenuDetail find(Long roleId, Long menuDetailId) {
        final String sql = " select * from wcm_common_role_menu_detail where role_id = ? and menu_detail_id = ? ";
        return this.jrJdbcTemplate.getJdbcTemplate().queryForObject(sql, new Object[]{roleId, menuDetailId}, new WcmCommonRoleMenuDetailRowerMapper());
    }

    @Override
    public void update(WcmCommonRoleMenuDetail entity) {
        final String sql = " update wcm_common_role_menu_detail set permissions = ? , disabled = ?  where role_id = ? and menu_detail_id = ? ";
        this.jrJdbcTemplate.getJdbcTemplate().update(sql, new Object[]{
                entity.getPermissions(), entity.getDisabled(), entity.getRoleId(), entity.getMenuDetailId()
        });
    }

    @Override
    public List<WcmCommonRoleMenuDetail> find(Long roleId, Long[] menuDetailIds) {
        String whereStr = "";
        Object[] params = null;
        if (null != menuDetailIds && menuDetailIds.length > 0) {
            params = new Object[menuDetailIds.length + 2];
            params[0] = roleId;
            params[1] = 0;
            for (int i = 0; i < menuDetailIds.length; i++) {
                whereStr += " OR menu_detail_id = ? ";
                params[i + 2] = menuDetailIds[i];
            }
        } else {
            return null;
        }
        final String sql = " select * from wcm_common_role_menu_detail where role_id = ? and ( menu_detail_id = ? " + whereStr + ") ";
        return this.jrJdbcTemplate.getJdbcTemplate().query(sql, params, new WcmCommonRoleMenuDetailRowerMapper());
    }
}

class WcmCommonRoleMenuDetailRowerMapper implements RowMapper<WcmCommonRoleMenuDetail> {
    @Override
    public WcmCommonRoleMenuDetail mapRow(ResultSet rs, int rowNum) throws SQLException {
        ResultSetMetaData rsmd = rs.getMetaData();
        int columnCount = rsmd.getColumnCount();
        Set<String> fieldSet = new HashSet<String>();
        for (int i = 1; i <= columnCount; i++) {
            fieldSet.add(rsmd.getColumnName(i));
        }
        WcmCommonRoleMenuDetail entity = new WcmCommonRoleMenuDetail();
        if (fieldSet.contains("role_id") && null != rs.getObject("role_id")) {
            entity.setRoleId(rs.getLong("role_id"));
        }
        if (fieldSet.contains("menu_detail_id") && null != rs.getObject("menu_detail_id")) {
            entity.setMenuDetailId(rs.getLong("menu_detail_id"));
        }
        if (fieldSet.contains("disabled") && null != rs.getObject("disabled")) {
            entity.setDisabled(rs.getBoolean("disabled"));
        }
        if (fieldSet.contains("permissions") && null != rs.getObject("permissions")) {
            entity.setPermissions(rs.getString("permissions"));
        }
        return entity;
    }
}
